package com.zekdot.registerclient.service.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zekdot.registerclient.dao.EnrollDataDao;
import com.zekdot.registerclient.entity.EnrollDataEntity;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Create by zekdot on 20-4-12.
 */
@Component

public class EnrollReciver {
    @Resource
    private EnrollDataDao enrollDataDao;
    @RabbitHandler
    @RabbitListener(queues = "EnrollQueue")
    public void process(String enrollStr){
        EnrollDataEntity enrollDataEntity = JSONObject.parseObject(enrollStr,EnrollDataEntity.class);

        EnrollDataEntity enrollDataPre = enrollDataDao.getEnrollStatus(enrollDataEntity.getUsername(),enrollDataEntity.getEId());
        // 如果已经预报名了
        if(enrollDataPre != null && enrollDataPre.getStatus() == 3) {
            // 使用Update语句更新
            enrollDataDao.update(enrollDataEntity);
        }else if(enrollDataPre == null){
            // 使用Insert语句插入
            enrollDataDao.save(enrollDataEntity);
        }
    }
}
